from django.shortcuts import render, redirect,reverse
from publishing import models


# Create your views here.
def delete(request, table, pk):
    getattr(models, table.capitalize()).objects.get(pk=pk).delete()
    return redirect(reverse(table))

def publisher(request):
    publisher_list = models.Publisher.objects.all().order_by('pid')
    return render(request, 'publisher.html', {'publisher_list': publisher_list})


def add_publisher(request):
    error = ''
    if request.method == 'POST':
        publisher = request.POST['publisher'].strip()
        obj = models.Publisher.objects.filter(name=publisher)
        if obj:
            error = '已存在'
        elif not publisher:
            error = '不能为空'
        else:
            models.Publisher.objects.create(name=publisher)
            return redirect('/publisher/')
    return render(request, 'add_publisher.html', {'error': error})


def del_publisher(request):
    pk = request.GET.get('id')
    obj = models.Publisher.objects.get(pk=pk)
    obj.delete()
    return redirect('/publisher/')


def alter_publisher(request):
    error = ''
    pk = request.GET.get('id')
    obj = models.Publisher.objects.get(pk=pk)
    if request.method == 'POST':
        publisher = request.POST['publisher'].strip()
        publisher_list = models.Publisher.objects.filter(name=publisher)
        if obj.name == publisher:
            error = '无修改'
        elif publisher_list:
            error = '已存在'
        else:
            obj.name = publisher
            obj.save()
            return redirect('/publisher/')
    return render(request, 'alter_publisher.html', {'error': error, 'obj': obj})


def book(request):
    book_list = models.Book.objects.all().order_by('id')
    return render(request, 'book.html', {'book_list': book_list})


# def add_book(request):
#     error = ''
#     pub_list = models.Publisher.objects.all().order_by('pid')
#     auther_list = models.Auther.objects.all()
#     if request.method == 'POST':
#         book = request.POST['book']
#         pub_id = request.POST['pub_id']
#         obj = models.Book.objects.filter(book_name=book)
#         if obj:
#             error = '书名已存在;'
#         else:
#             models.Book.objects.create(book_name=book, pub_id=pub_id)
#             error = '添加成功'
#     return render(request, 'add_book.html', {'pub_list': pub_list, 'error': error, 'auther_list': auther_list})

from django.views import View
from django.utils.decorators import method_decorator


class Addbook(View):
    def get(self, request):
        pub_list = models.Publisher.objects.all().order_by('pid')
        auther_list = models.Auther.objects.all()
        return render(request, 'add_book.html', {'pub_list': pub_list, 'auther_list': auther_list})

    def post(self, request):
        pub_list = models.Publisher.objects.all().order_by('pid')
        auther_list = models.Auther.objects.all()
        book = request.POST['book']
        pub_id = request.POST['pub_id']
        obj = models.Book.objects.filter(book_name=book)
        if obj:
            error = '书名已存在;'
        else:
            models.Book.objects.create(book_name=book, pub_id=pub_id)
            error = '添加成功'
        return render(request, 'add_book.html', {'pub_list': pub_list, 'error': error, 'auther_list': auther_list})


def alter_book(request, pk):
    error = ''
    # pk = request.GET.get('id')
    book = models.Book.objects.get(pk=pk)
    pub_list = models.Publisher.objects.all()
    auther_list = models.Auther.objects.all()
    book_auther = models.Auther_Book.objects.filter(book_id=pk)
    book_auther_list = []
    for i in book_auther:
        book_auther_list.append(i.auther_id)
    if request.method == 'POST':
        book_name = request.POST['book'].strip()
        pub_id = int(request.POST['pub_id'])
        # if pub_id == book.pub_id and book_name == book.book_name:
        #     error = '无修改'
        #     return render(request, 'alter_book.html',
        #                   {'pub_list': pub_list, 'book': book, 'error': error, 'auther_list': auther_list,
        #                    'book_auther_list': book_auther_list})
        book.book_name = book_name
        book.pub_id = pub_id
        book.save()
        return redirect('/book/')
    return render(request, 'alter_book.html',
                  {'pub_list': pub_list, 'book': book, 'error': error, 'auther_list': auther_list})


def del_book(request, pk):
    obj = models.Book.objects.get(pk=pk)
    obj.delete()
    return redirect('/book/')


def auther(request):
    auther_list = models.Auther.objects.all()
    return render(request, 'auther.html', {'auther_list': auther_list})


def add_auther(request):
    error = ''
    if request.method == 'POST':
        auther = request.POST['auther'].strip()
        if models.Auther.objects.filter(name=auther):
            error = '作者已存在'
        else:
            models.Auther.objects.create(name=auther)
            redirect('/auther/')
    return render(request, 'add_auther.html', {'error': error})


def alter_auther(request):
    error = ''
    pk = request.GET.get('id')
    obj = models.Auther.objects.get(pk=pk)
    if request.method == 'POST':
        auther = request.POST['auther'].strip()
        if models.Auther.objects.filter(name=auther):
            error = '作者已存在'
        if not error:
            obj.name = auther
            obj.save()
            return redirect('/auther/')
    return render(request, 'alter_auther.html', {'error': error, 'obj': obj})


def del_auther(request):
    pk = request.GET.get('id')
    models.Auther.objects.get(pk=pk).delete()
    return redirect('/auther/')


def base(request):
    return render(request, 'base.html')
